package com.mrjy.queries.bo;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.mrjy.common.entity.UserQuestionRel;
import com.mrjy.queries.dao.IQuestionInfoDao;
import com.mrjy.queries.dao.IUserQuestionRelDao;

@Service("UserQuestionRelBo")
public class UserQuestionRelBo implements IUserQuestionRelBo {
	@Resource
	private IUserQuestionRelDao userQuestionRelDao;
	@Resource
	private IQuestionInfoDao questionInfoDao;

	@Override
	public Object txInsert(UserQuestionRel rel) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(null == rel){
			map.put("err_code", -1);
			map.put("err_msg", "传进来的对象为空");
			return map;
        } else if (rel.getUserId() == null) {
            map.put("err_code", -2);
            map.put("err_msg", "传进来的用户Id为空");
            return map;
        }
        rel.setCreateTime(new Date());
		int count = userQuestionRelDao.insert(rel);
		if (count > 0) {
			//回写questioninfo表的浏览量
			int qCount = questionInfoDao.addQuestionViewNum(rel.getQuestionId());
			map.put("err_code", 0);
			map.put("err_msg", "插入成功");
		}else {
			map.put("err_code", 1);
			map.put("err_msg", "插入失败");
		}
		return map;	
	}

	@Override
	public Object txUpdateById(UserQuestionRel rel) {
		return userQuestionRelDao.updateById(rel);
	}

	@Override
	public Object queryByUserIdAndQuestionId(UserQuestionRel rel) {
		Map<String, Object> map = new HashMap<String, Object>();
		int count = userQuestionRelDao.queryByUserIdAndQuestionId(rel);
		if(count > 0){
			map.put("err_code", 1);
			map.put("err_msg", "查询成功有数据");
		}else {
			map.put("err_code", 0);
			map.put("err_msg", "查询成功无数据");
		}
		return map;
	}
}
